package com.tec.modules.order.rest;

import com.tec.modules.order.domain.Stock;
import com.tec.modules.order.service.StockService;
import com.tec.modules.order.service.dto.StockQueryCriteria;
import me.zhengjie.annotation.Log;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author tec
* @date 2020-05-27
*/
@Api(tags = "销售管理：库存一览")
@RestController
@RequestMapping("/api/stock")
public class StockController {

    private final StockService stockService;

    public StockController(StockService stockService) {
        this.stockService = stockService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('stock:list')")
    public void download(HttpServletResponse response, StockQueryCriteria criteria) throws IOException {
        stockService.download(stockService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询库存")
    @ApiOperation("查询库存")
    @PreAuthorize("@el.check('stock:list')")
    public ResponseEntity<Object> getStocks(StockQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(stockService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增库存")
    @ApiOperation("新增库存")
    @PreAuthorize("@el.check('stock:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody Stock resources){
        return new ResponseEntity<>(stockService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改库存")
    @ApiOperation("修改库存")
    @PreAuthorize("@el.check('stock:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody Stock resources){
        stockService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除库存")
    @ApiOperation("删除库存")
    @PreAuthorize("@el.check('stock:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Long[] ids) {
        stockService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
